const globals = {};

const stackBinder = makeBindFun(function (stack, v) {
    stack.push(v);
    return stack;
});

const stackUnbinder = makeBindFun(function (stack) {
    stack.pop();
    return stack;

});

function makeBindFun(resolver) {
    return function (k, v) {
        const stack = globals[k] || [];
        globals[k] = resolver(stack, v);
    };
};

const target = {
    name: 'the right value',
    aux: function () {
        return this.name;
    },
    act: function () {
        return this.aux();
    }
}

try {
    console.log(target.act.call('123'));
} catch (e) {
    console.log('Error');
}

bindAll(target, 'aux', 'act');
console.log(target.act.call('123'));


function bindAll(obj, ...keys) {
    keys.forEach(key => {
        bind(obj, key);
    });

    return obj;
}

function bind(obj, key) {
    const param = obj[key];
    if (typeof param === 'function') {
        obj[key] = param.bind(target);
    }
    return obj;
}